package com.summer.safe.config.mybatis;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.summer.safe.utils.Sm3Utils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * mybatis配置
 */
@Configuration
public class MyBatisConfig {

    @Autowired
    private Environment environment;
    /**
     * 获得配置文件中的属性
     */
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean(name = "dataSource")
    public DruidDataSource druidDataSource() {
        return DruidDataSourceBuilder.create().build();
    }



    /**
     * sql会话工厂
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DruidDataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        //解密数据库名称
        dataSource.setUsername(Sm3Utils.setUsername(dataSource.getUsername()));
        //解密数据库密码
        dataSource.setPassword(Sm3Utils.setPassword(dataSource.getPassword()));
        bean.setDataSource(dataSource);

        // 加载全局的配置文件
        bean.setTypeAliasesPackage(environment.getProperty("mybatis.typeAliasesPackage"));
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources(environment.getProperty("mybatis.mapperLocations"));
        //bean.setTypeAliasesPackage(environment.getProperty("mybatis.typeAliasesPackage"));

        //Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml");
        bean.setMapperLocations(resources);
        //重新加载mybatis配置
        bean.setVfs(SpringBootVFS.class);
        return bean.getObject();
    }


    @Bean
    public ServletRegistrationBean druidServlet(@Qualifier("dataSource") DruidDataSource dataSource) {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
       /* reg.addInitParameter("loginUsername", Sm3Utils.setUsername(dataSource.getUsername()));
        reg.addInitParameter("loginPassword", Sm3Utils.setPassword(dataSource.getPassword()));
        */

        return reg;
    }
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        return filterRegistrationBean;
    }

    /**
     * Spring事务管理
     * @param dataSource
     * @return
     */
    @Bean
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DruidDataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * 代理以往的DefailtSqlSession完成对数据库的操作
     * @param sqlSessionFactory
     * @return
     */
    @Bean
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
